/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import pojos.BillItem;
import dao.exceptions.NonexistentEntityException;
import dao.exceptions.PreexistingEntityException;
import dao.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import pojos.Item;
import pojos.Bill;
import javax.transaction.UserTransaction;

/**
 *
 * @author A7med Kotb
 */
public class BillItemJpaController implements Serializable {

    public BillItemJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(BillItem billItem) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Item itemID = billItem.getItemID();
            if (itemID != null) {
                itemID = em.getReference(itemID.getClass(), itemID.getIdItem());
                billItem.setItemID(itemID);
            }
            Bill billID = billItem.getBillID();
            if (billID != null) {
                billID = em.getReference(billID.getClass(), billID.getIdBill());
                billItem.setBillID(billID);
            }
            em.persist(billItem);
            if (itemID != null) {
                itemID.getBillItemCollection().add(billItem);
                itemID = em.merge(itemID);
            }
            if (billID != null) {
                billID.getBillItemCollection().add(billItem);
                billID = em.merge(billID);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findBillItem(billItem.getIdBillItem()) != null) {
                throw new PreexistingEntityException("BillItem " + billItem + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(BillItem billItem) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            BillItem persistentBillItem = em.find(BillItem.class, billItem.getIdBillItem());
            Item itemIDOld = persistentBillItem.getItemID();
            Item itemIDNew = billItem.getItemID();
            Bill billIDOld = persistentBillItem.getBillID();
            Bill billIDNew = billItem.getBillID();
            if (itemIDNew != null) {
                itemIDNew = em.getReference(itemIDNew.getClass(), itemIDNew.getIdItem());
                billItem.setItemID(itemIDNew);
            }
            if (billIDNew != null) {
                billIDNew = em.getReference(billIDNew.getClass(), billIDNew.getIdBill());
                billItem.setBillID(billIDNew);
            }
            billItem = em.merge(billItem);
            if (itemIDOld != null && !itemIDOld.equals(itemIDNew)) {
                itemIDOld.getBillItemCollection().remove(billItem);
                itemIDOld = em.merge(itemIDOld);
            }
            if (itemIDNew != null && !itemIDNew.equals(itemIDOld)) {
                itemIDNew.getBillItemCollection().add(billItem);
                itemIDNew = em.merge(itemIDNew);
            }
            if (billIDOld != null && !billIDOld.equals(billIDNew)) {
                billIDOld.getBillItemCollection().remove(billItem);
                billIDOld = em.merge(billIDOld);
            }
            if (billIDNew != null && !billIDNew.equals(billIDOld)) {
                billIDNew.getBillItemCollection().add(billItem);
                billIDNew = em.merge(billIDNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = billItem.getIdBillItem();
                if (findBillItem(id) == null) {
                    throw new NonexistentEntityException("The billItem with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            BillItem billItem;
            try {
                billItem = em.getReference(BillItem.class, id);
                billItem.getIdBillItem();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The billItem with id " + id + " no longer exists.", enfe);
            }
            Item itemID = billItem.getItemID();
            if (itemID != null) {
                itemID.getBillItemCollection().remove(billItem);
                itemID = em.merge(itemID);
            }
            Bill billID = billItem.getBillID();
            if (billID != null) {
                billID.getBillItemCollection().remove(billItem);
                billID = em.merge(billID);
            }
            em.remove(billItem);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<BillItem> findBillItemEntities() {
        return findBillItemEntities(true, -1, -1);
    }

    public List<BillItem> findBillItemEntities(int maxResults, int firstResult) {
        return findBillItemEntities(false, maxResults, firstResult);
    }

    private List<BillItem> findBillItemEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(BillItem.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public BillItem findBillItem(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(BillItem.class, id);
        } finally {
            em.close();
        }
    }

    public int getBillItemCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<BillItem> rt = cq.from(BillItem.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
